package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.LeaveStatsDTO;
import com.ruoyi.system.mapper.LeaveapplyMapper;
import com.ruoyi.system.service.ILeaveStatsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class LeaveStatsServiceImpl implements ILeaveStatsService {

    @Resource
    private LeaveapplyMapper leaveapplyMapper;

    @Override
    public LeaveStatsDTO getLeaveStatistics(String type) {
        LeaveStatsDTO dto = new LeaveStatsDTO();

        // 获取基础统计数据
        int total = leaveapplyMapper.selectTotalCount();
        int lastMonth = leaveapplyMapper.selectLastMonthCount();
        int approval = leaveapplyMapper.selectApprovalCount();

        dto.setTotal(total);
        dto.setLastMonth(lastMonth);
        dto.setApproval(approval);

        // 设置增长率
        dto.setTotalRate(calculateGrowthRate(total));
        dto.setLastMonthRate(calculateGrowthRate(lastMonth));
        dto.setApprovalRate(calculateGrowthRate(approval));

        // 获取待办任务数
        String userId = null;
        // 如果不是管理员，只查看自己的待办
        if (!SecurityUtils.isAdmin(SecurityUtils.getLoginUser().getUser().getUserId())) {
            userId = SecurityUtils.getUsername();
        }
        int pendingTasks = leaveapplyMapper.selectPendingTaskCount(userId);
        int lastMonthPendingTasks = leaveapplyMapper.selectLastMonthPendingTaskCount(userId);
        
        dto.setPendingTasks(pendingTasks);
        // 计算待办任务环比增长率
        if (lastMonthPendingTasks > 0) {
            double rate = ((double)(pendingTasks - lastMonthPendingTasks) / lastMonthPendingTasks) * 100;
            dto.setPendingRate(Math.round(rate * 100.0) / 100.0); // 保留两位小数
        } else {
            dto.setPendingRate(0.0);
        }

        // 根据类型获取图表数据
        List<Map<String, Object>> chartData;
        switch (type) {
            case "天":
                chartData = leaveapplyMapper.selectDailyChartData();
                break;
            case "月":
                chartData = leaveapplyMapper.selectMonthlyChartData();
                break;
            case "年":
                chartData = leaveapplyMapper.selectYearlyChartData();
                break;
            default:
                chartData = leaveapplyMapper.selectDailyChartData();
        }
        dto.setChartData(chartData);

        return dto;
    }

    private Double calculateGrowthRate(Integer current) {
        // 这里使用模拟数据，实际应该根据历史数据计算
        return current > 0 ? (current * 0.1) : 0.0;
    }

    /**
     * 获取最近的请假申请列表
     */
    public List<Map<String, Object>> getRecentLeaveApplyList() {
        Long deptId = null;
        String userId = null;
        
        // 获取当前登录用户
        Long loginUserId = SecurityUtils.getLoginUser().getUser().getUserId();
        
        if (SecurityUtils.isAdmin(loginUserId)) {
            // 如果是管理员，不设置过滤条件，可以查看所有申请
        } else {
            // 如果是普通用户，查看：
            // 1. 自己发起的申请
            // 2. 自己处理过的任务
            userId = SecurityUtils.getUsername();
        }
        
        return leaveapplyMapper.selectRecentLeaveApplyList(deptId, userId);
    }
} 